package com.richdown.movie.util;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.StringWriter;

import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;

import com.richdown.movie.model.FetionReq;

/**
 * 类 <code>BeanToXml.java</code> 把javabean转换成xml
 * 
 * @author liubaojian@feinno.com
 * @version v1.0
 * @date 2010-2-22
 */
public class BeanToXml {

	/**
	 * 通过bean够着xml格式的数据,这个只作为向ss端请求使用
	 * 
	 * @param fetionreq请求对象
	 * @return StringWriter
	 * @throws IntrospectionException
	 * @throws SAXException
	 * @throws IOException
	 */
	public static String getXmlFromBean(FetionReq fetionreq)
			throws IOException, SAXException, IntrospectionException {
		StringWriter outputWriter = new StringWriter();
		outputWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		BeanWriter beanWriter = new BeanWriter(outputWriter);
		beanWriter.getBindingConfiguration().setMapIDs(false);
		beanWriter.setEndOfLine("");
		beanWriter.write(fetionreq);
		String out = outputWriter.toString();
		out=out.replace("FetionReq", "fetion");
		outputWriter.close();
		return out;
	}

	/**
	 * 通过bean够着xml格式的数据,Ibmp端使用
	 * 
	 * @param uid用户的的uid
	 * @param bean转换的对象数组
	 * @return StringWriter
	 * @throws IntrospectionException
	 * @throws SAXException
	 * @throws IOException
	 */
	public static String getVipXmlFromBean(String uid) throws IOException,
			SAXException, IntrospectionException {
		StringWriter outputWriter = new StringWriter();
		outputWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		BeanWriter beanWriter = new BeanWriter(outputWriter);
		beanWriter.getXMLIntrospector().getConfiguration()
				.setAttributesForPrimitives(false);
		beanWriter.getBindingConfiguration().setMapIDs(false);
		beanWriter.enablePrettyPrint();
		beanWriter.write("uid", uid);
		beanWriter.write("servId", "50");
		String out = outputWriter.toString();
		outputWriter.close();
		return out;
	}
}
